package com.san.hkhospital.controller;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.san.hkhospital.entity.Payin;
import com.san.hkhospital.entity.Payout;
import com.san.hkhospital.service.PayinService;
import com.san.hkhospital.service.PayoutService;
import com.san.hkhospital.service.impl.PayinServiceImpl;

import com.san.hkhospital.service.impl.PayoutServiceImpl;
import com.san.hkhospital.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (payin)表控制层
 *
 * @author xxxxx
 */
@RestController
@RequestMapping("/payout")
public class PayoutController {
    /**
     * 服务对象
     */
    @Resource
    private PayoutServiceImpl payoutServiceImpl;
    @Autowired
    private PayoutService payoutService;
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
//    @GetMapping("selectOne")
//    public Payout selectOne(Integer id) {
//        return payoutServiceImpl.selectByPrimaryKey(id);
//    }



    @GetMapping("payOutList")
    public ResultVO payOutList(@RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue="5")Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Payout> payOutList = payoutService.payOutList();
        PageInfo<Payout> pageInfo = new PageInfo<Payout>(payOutList);
        return ResultVO.success("所有支出查看成功",pageInfo);
    }

    @GetMapping("/searchPayOut")
    public ResultVO searchPayOut( @RequestParam Map<String, String> params,
                                  @RequestParam(value = "startDate", required = false) String startDateStr,
                                  @RequestParam(value = "endDate", required = false) String endDateStr,
                                  @RequestParam(defaultValue = "1") Integer pageNum,
                                  @RequestParam(defaultValue = "5") Integer pageSize) {
        try {
            if (startDateStr != null && endDateStr != null) {
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                LocalDateTime startDate = LocalDateTime.parse(startDateStr, formatter);
                LocalDateTime endDate = LocalDateTime.parse(endDateStr, formatter);
                params.put("startDate", startDate.toString());
                params.put("endDate", endDate.toString());
            }

            PageHelper.startPage(pageNum, pageSize);
            List<Payout> searchList = payoutService.searchList(params);
            PageInfo<Payout> pageInfo = new PageInfo<Payout>(searchList);
            return ResultVO.success("搜索查询成功", pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.failed("搜索查询失败");
        }
    }


    @PostMapping("addPayOut")
    public ResultVO addPayOut(@RequestBody Payout payout){
        System.out.println("Received Payout: " + payout);
        boolean flag = payoutService.addPayOut(payout);
        if(flag){
            return ResultVO.success("支出记录添加成功");
        }
        return  ResultVO.failed("支出记录添加失败");
    }



    @PostMapping("/updatePayOut/{id}")
    public ResultVO updatePayOut(@PathVariable int id, @RequestBody Payout payout){
        payout.setOutId(id);
        int flag = payoutService.updatePayOut(payout);
        if(flag==0){
            return ResultVO.failed("支出记录修改失败");
        }
        return  ResultVO.success("支出记录修改成功");
    }






}
